﻿using UnityEngine;
using System.Collections;

public class BuildingShadow : MonoBehaviour {
    bool m_bShadowAlphaToZero;
    bool m_bShadowAlphaToOne;

    public bool bStaticBuilding;
	// Use this for initialization
	void Start () {
        GetComponent<SpriteRenderer>().sprite = transform.parent.GetComponent<SpriteRenderer>().sprite;
        GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, 100 / 255f);
	}
	
	// Update is called once per frame
	void Update () {
        transform.localPosition = transform.parent.localPosition;

        if (Clock.iHour == 5)
            m_bShadowAlphaToOne = true;

        if (Clock.iHour >= 5 && Clock.iHour <= 20)
        {
            transform.localPosition = Vector3.zero - Vector3.Normalize(GameObject.Find("SUN").transform.position - transform.parent.position) * 0.015f;
            if (bStaticBuilding)
                transform.localPosition = Vector3.zero - Vector3.Normalize(GameObject.Find("SUN").transform.position - transform.parent.position) * 0.15f;
        }
        else
        {
            GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, 0);
            m_bShadowAlphaToZero = false;
        }

        if (Clock.iHour == 17)
            m_bShadowAlphaToZero = true;

        if (m_bShadowAlphaToZero)
            GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, GetComponent<SpriteRenderer>().color.a - (0.25f * Time.deltaTime));
        else if (m_bShadowAlphaToOne)
            if (GetComponent<SpriteRenderer>().color.a < 100 / 255f)
                GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, GetComponent<SpriteRenderer>().color.a + (0.25f * Time.deltaTime));
            else
            {
                GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, 100/255f);
                m_bShadowAlphaToOne = false;
            }
	}
}
